<template>
  <el-dialog
    class="check-tab"
    :close-on-click-modal="false"
    top="5vh"
    width="85%"
    :title="title"
    :visible.sync="visible"
    :before-close="close"
    lock-scroll
    append-to-body
  >
    <el-tabs v-if="!isYear">
      <el-tab-pane v-for="(tab, index) in tabsTitle" :label="tab" :key="tab">
        <el-table
          show-summary
          :summary-method="(params) => getSummaries(params, index)"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          :data="index == 0 ? datas.normal : datas.quality"
          border
          max-height="380"
          size="mini"
          style="width: 100%"
        >
          <el-table-column
            v-for="col in columns"
            :label="col.label"
            :prop="col.prop"
            :key="col.label"
            :width="col.width ? col.width : null"
          >
            <template slot-scope="scope">
              <span class="html-content" v-html="scope.row[col.prop]"></span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <el-table
      v-else
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      :data="datas.tableYear"
      border
      max-height="380"
      size="mini"
      style="width: 100%"
    >
      <el-table-column
        v-for="column in columns2"
        :key="column.props"
        :prop="column.props"
        :label="column.label"
        :width="column.width ? column.width : ''"
      >
        <template slot-scope="scope">
          <div v-if="column.isTemplate">
            <div v-if="column.quarterLevel">
              <span v-if="scope.row[column.props]">{{
                scope.row[column.props] +
                "（" +
                scope.row[column.levelScope] +
                "）/ " +
                scope.row[column.ownScore]
              }}</span>
              <span v-else>--</span>
            </div>
            <div v-if="column.quarterScore">
              {{ scope.row[column.props] ? scope.row[column.props] : "--" }}
            </div>
          </div>
          <div v-else>
            {{scope.row[column.props]}}
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "addEdit",
  props: {
    visible: Boolean,
    title: String,
    datas: {
      type: Object,
    },
    isYear: Boolean,
  },
  data() {
    return {
      tabsTitle: ["日常考核标准", "质量考核标准"],
      columns: [
        {
          prop: "month",
          label: "时间",
          width: "80",
        },
        {
          prop: "name",
          label: "考核标准",
        },
        {
          prop: "score",
          label: "所得分数",
          width: "80",
        },
      ],
      columns2: [
        {
          props: "name",
          label: "姓名",
          width: "75",
          isTemplate: false,
          quarterLevel: false,
          quarterScore: false,
        },
        {
          props: "quarterOneLevel",
          levelScope: "quarterOneScope",
          ownScore: "quarterOneScore",
          label: "一季度 等级（参考范围）/ 得分",
          isTemplate: true,
          quarterLevel: true,
          quarterScore: false,
        },
        {
          props: "quarterTwoLevel",
          levelScope: "quarterTwoScope",
          ownScore: "quarterTwoScore",
          label: "二季度 等级（参考范围）/ 得分",
          isTemplate: true,
          quarterLevel: true,
          quarterScore: false,
        },
        {
          props: "quarterThreeLevel",
          levelScope: "quarterThreeScope",
          ownScore: "quarterThreeScore",
          label: "三季度 等级（参考范围）/ 得分",
          isTemplate: true,
          quarterLevel: true,
          quarterScore: false,
        },
        {
          props: "quarterFourLevel",
          levelScope: "quarterFourScope",
          ownScore: "quarterFourScore",
          label: "四季度 等级（参考范围）/ 得分",
          isTemplate: true,
          quarterLevel: true,
          quarterScore: false,
        },
      ],
    };
  },
  computed: {},
  components: {},
  created() {
    console.log(this.datas);
  },
  methods: {
    close() {
      this.$emit("update:visible", false);
    },
    getSummaries(param, index) {
      const { columns, data } = param;
      const sums = [];
      sums[0] = "总计";
      if (index === 0) {
        // 日常考核得分
        const values = data.map((item) => Number(item["score"]));
        if (!values.every((value) => isNaN(value))) {
          sums[columns.length - 1] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[columns.length - 1] = (sums[columns.length - 1] * 0.7).toFixed(
            2
          );
          sums[columns.length - 1] += " 分";
        } else {
          sums[columns.length - 1] = "N/A";
        }
      } else if (index === 1) {
        // const values = data.map(item=>Number(item['score']))
        let dataArr = JSON.parse(JSON.stringify(data));
        const special = dataArr.filter((item) => {
          if (item.spot_id === 92 || item.spot_id === 206) return item;
        });
        if (special.length > 1) {
          let max = undefined;
          special.map((i) => {
            if (max) {
              if (i.score > max.score) {
                max = i;
              }
            } else {
              max = i;
            }
          });
          dataArr.map((item) => {
            if (item.spot_id === 92 || item.spot_id === 206) {
              if (item.id !== max.id) {
                item.score = 0;
              }
            }
          });
        }

        const values = dataArr
          .filter((item) => item.weight !== 100)
          .map((i) => Number(i["score"]));
        const valuesWeight = dataArr
          .filter((item) => item.weight === 100)
          .map((i) => Number(i["score"]));
        let sum = this.sum(values) * 0.3 + 30;
        let sumWeight = this.sum(valuesWeight);
        sums[columns.length - 1] = (sum + sumWeight).toFixed(2);
        sums[columns.length - 1] += " 分";
      }

      return sums;
    },
    sum(values) {
      let sums = 0;
      if (!values.every((value) => isNaN(value))) {
        sums = values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0);
      } else {
        sums = 0;
      }
      return sums;
    },
  },
};
</script>
<style lang="scss">
.check-tab > .el-dialog > .el-dialog__body {
  padding: 0px 20px 10px !important;
}
</style>